package com.bw.paper.mvp.view.Activity;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;
import com.bw.paper.App;
import com.bw.paper.R;
import com.bw.paper.entity.ResponseUserEntity;
import com.bw.paper.entity.UserEntity;
import com.bw.paper.mvp.base.model.BaseEntity;
import com.bw.paper.mvp.base.view.BaseActivity;
import com.bw.paper.mvp.contact.LoginContract;
import com.bw.paper.mvp.di.DaggerLoginComponent;
import com.bw.paper.mvp.di.LoginModules;
import com.bw.paper.mvp.model.HttpCode;
import com.bw.paper.mvp.model.LoginModel;
import com.bw.paper.mvp.presenter.LoginPresenter;

public class LoginActivity extends BaseActivity<LoginPresenter>implements
        LoginContract.LoginView,View.OnClickListener{


    private ImageView headBackImg;
    private LinearLayout vanish;
    private EditText etPhone;
    private EditText etPwd;
    private ImageView loginShowImg;
    private Button numberBt;
    private TextView textQt2;
    private LinearLayout vanish2;
    private EditText etPhone2;
    private EditText etPwd2;
    private Button buttonBt;
    private TextView textQt;
    private ImageView loginShowImg2;

    private UserEntity values;
    private  LoginPresenter  mPresenter;

    @Override
    public View addStatusView() {
        return f(R.id.login_linear);
    }


    @Override
    public int bindlayout() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {

        headBackImg = (ImageView) findViewById(R.id.head_back_img);

        vanish = (LinearLayout) findViewById(R.id.vanish);
        etPhone = (EditText) findViewById(R.id.et_phone);
        etPwd = (EditText) findViewById(R.id.et_pwd);
        loginShowImg = (ImageView) findViewById(R.id.login_show_img);
        numberBt = (Button) findViewById(R.id.number_bt);
        textQt2 = (TextView) findViewById(R.id.text_qt2);


        vanish2 = (LinearLayout) findViewById(R.id.vanish2);
        etPhone2 = (EditText) findViewById(R.id.et_phone2);
        etPwd2 = (EditText) findViewById(R.id.et_pwd2);
        buttonBt = (Button) findViewById(R.id.button_bt);
        textQt = (TextView) findViewById(R.id.text_qt);
        loginShowImg2 = (ImageView) findViewById(R.id.login_show_img2);



        loginShowImg.setOnClickListener(this::onClick);
        headBackImg.setOnClickListener(this::onClick);

        numberBt.setOnClickListener(this::onClick);
        textQt2.setOnClickListener(this::onClick);
        loginShowImg.setOnClickListener(this::onClick);

        buttonBt.setOnClickListener(this::onClick);
        textQt.setOnClickListener(this::onClick);
        loginShowImg2.setOnClickListener(this::onClick);

    }

    @Override
    public void initData() {
          mPresenter = new LoginPresenter(new LoginModel(),this);
    }

    @Override
    public void inject() {
        DaggerLoginComponent.builder().loginModules(new LoginModules(this))
                .build().injectLogin(this);
    }

    @Override
    public void refreshView(BaseEntity entity) {
        ResponseUserEntity entity1 = (ResponseUserEntity) entity;
        values = entity1.getValues();
        if (entity1.status.equals("200")) {
            int user_id = values.getUser_id();
            String user_img = values.getUser_img();
            int user_integral = values.getUser_integral();
            String user_name = values.getUser_name();
            int user_phone = values.getUser_phone();
            String user_pwd = values.getUser_pwd();
            String user_sex = values.getUser_sex();
            try {
                App.getInstance().getDaoSession().insert(new UserEntity(user_id,user_phone,user_pwd,user_name,user_sex,user_img,user_integral));
                startPage(UpUserActivity.class);
                finish();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                LogUtils.json(
                        App.getInstance().getDaoSession().loadAll(UserEntity.class)
                );

            } catch (Exception e) {
                e.printStackTrace();
            }

    } else {
            Toast.makeText(this, ""+entity.message, Toast.LENGTH_SHORT).show();
        }
    }
    int index;
    @Override
    public void onClick(View v) {
        int id = v.getId();
//        返回
        if (R.id.head_back_img == id){
            finish();
        }
//        切换其他登录方式
        if (R.id.text_qt2 == id){
            AnimatorSet animatorSet = new AnimatorSet();
            ObjectAnimator translationY = ObjectAnimator.ofFloat(vanish, "translationY", 0, 200);
            ObjectAnimator alpha = ObjectAnimator.ofFloat(vanish, "alpha", 1, 0);
            animatorSet.setInterpolator(new LinearInterpolator());
            animatorSet.setDuration(2000);
            animatorSet.playTogether(translationY,alpha);
            animatorSet.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {

                    vanish.setVisibility(View.GONE);
                    vanish2.setVisibility(View.VISIBLE);
                    etPhone.setText("");
                    etPwd.setText("");

                    AnimatorSet animatorSet1 = new AnimatorSet();
                    ObjectAnimator alpha1 = ObjectAnimator.ofFloat(vanish2, "alpha", 0, 1);
                    ObjectAnimator translationY1 = ObjectAnimator.ofFloat(vanish2, "translationY", 200, 0);
                    animatorSet1.setInterpolator(new LinearInterpolator());
                    animatorSet1.setDuration(2000);
                    animatorSet1.playTogether(translationY1,alpha1);
                    animatorSet1.start();
                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
            animatorSet.start();

        }
//        手机号登录
        if (R.id.text_qt == id){
            AnimatorSet animatorSet = new AnimatorSet();
            ObjectAnimator translationY = ObjectAnimator.ofFloat(vanish2, "translationY", 0, 200);
            ObjectAnimator alpha = ObjectAnimator.ofFloat(vanish2, "alpha", 1, 0);
            animatorSet.setInterpolator(new LinearInterpolator());
            animatorSet.setDuration(2000);
            animatorSet.playTogether(translationY,alpha);
            animatorSet.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {

                    vanish2.setVisibility(View.GONE);
                    vanish.setVisibility(View.VISIBLE);
                    etPhone2.setText("");
                    etPwd2.setText("");

                    AnimatorSet animatorSet1 = new AnimatorSet();
                    ObjectAnimator alpha1 = ObjectAnimator.ofFloat(vanish, "alpha", 0, 1);
                    ObjectAnimator translationY1 = ObjectAnimator.ofFloat(vanish, "translationY", 200, 0);
                    animatorSet1.setInterpolator(new LinearInterpolator());
                    animatorSet1.setDuration(2000);
                    animatorSet1.playTogether(translationY1,alpha1);
                    animatorSet1.start();
                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
            animatorSet.start();



        }
//        其他登录按钮
        if (R.id.number_bt == id){
            String s = etPhone.getText().toString();
            String s1 = etPwd.getText().toString();
            if (s.equals("")||s1.equals("")){
                Toast.makeText(LoginActivity.this, "请输入电话号码或验证码", Toast.LENGTH_SHORT).show();
            }else if (s.length()>=11){
                Toast.makeText(LoginActivity.this, "电话号码输入不正确", Toast.LENGTH_SHORT).show();
            } else{
                int p = Integer.parseInt(s);
                mPresenter.requestLogin(p, s1, HttpCode.LOGINCODE);
            }
        }
        //        手机号登录 按钮
        if (R.id.button_bt == id){
            String s = etPhone2.getText().toString();
            String s1 = etPwd2.getText().toString();
            if (s.equals("") || s1.equals("")) {
                Toast.makeText(LoginActivity.this, "请输入电话号码或密码", Toast.LENGTH_SHORT).show();
            } else if (s.length() >= 11) {
                Toast.makeText(LoginActivity.this, "电话号码输入不正确", Toast.LENGTH_SHORT).show();
            } else {
                int p = Integer.parseInt(s);
                mPresenter.requestLogin(p, s1, HttpCode.REGISTCODE);
            }
        }

        if (R.id.login_show_img==id){

            if (index==0){
                index=1;
                loginShowImg.setImageResource(R.drawable.res_7icon_202);
                etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//隐藏密码

            }else
                if (index==1){
                index=0;
                    loginShowImg.setImageResource(R.drawable.res_7icon_201);
                    etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());//显示密码

                }
            etPwd.setSelection(etPwd.getText().toString().length());
        }
        if (R.id.login_show_img2==id){

            if (index==0){
                index=1;
                loginShowImg2.setImageResource(R.drawable.res_7icon_202);
                etPwd2.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//隐藏密码

            }else
                if (index==1){
                index=0;
                    loginShowImg2.setImageResource(R.drawable.res_7icon_201);
                    etPwd2.setTransformationMethod(PasswordTransformationMethod.getInstance());//显示密码

                }
            etPwd2.setSelection(etPwd2.getText().toString().length());
        }
    }
}
